package com.kgis.fm.common.manager
{
	import com.kgis.fm.common.component.legend.FmLegendDisplayControl;
	import com.kgis.fm.common.component.legend.control.LegendDisplayItem;
	import com.kgis.fm.flex.model.constant.LayerTypeConstant;
	import com.kgis.fm.flex.model.manager.FmMetaData;
	import com.kgis.fm.flex.model.term.LegendEntry;
	import com.kgis.fm.flex.model.term.LegendEntryDefinition;
	import com.kgis.fm.flex.model.term.LegendEntryDisplayControl;
	import com.kgis.map.Map;
	import com.kgis.map.layer.FeatureLayer;

	import mx.collections.ArrayCollection;

	/**
	 * 图层控制树管理
	 * */
	public class BusinessLegendTreeManager
	{
		public function BusinessLegendTreeManager()
		{
		}

		public static function initLegendTree(legendId:int, kgisMap:Map):void
		{
			var metaData:FmMetaData=MetaDataManager.getInstance().getMetaData(MetaDataManager.KEY_KGM_METAData) as FmMetaData;
			var i:int=0;
			var arrayDspCtrl:ArrayCollection=metaData.legendEntryDisplayMap[legendId] as ArrayCollection;
			var legendCtrl:FmLegendDisplayControl=new FmLegendDisplayControl();
			kgisMap.legendControl=legendCtrl;
			for each (var parentItem:LegendEntryDisplayControl in arrayDspCtrl)
			{
				if (parentItem.parentId == 0)
				{
					var rootItem:LegendDisplayItem=new LegendDisplayItem(parentItem.name);
					rootItem.legendEntryId=parentItem.leno;
					rootItem.legendEntryDspId=parentItem.id;

					if (parentItem.leno > 0)
					{
						var businessLegendEntryDef:LegendEntryDefinition=MetaDataManager.getLegendEntryDefByEntryId(legendId, parentItem.leno);
						rootItem.levelable=businessLegendEntryDef.scalable;
						rootItem.selectable=businessLegendEntryDef.selectable;
						rootItem.visible=businessLegendEntryDef.displayMode;
					}
					legendCtrl.addLegendItem(rootItem);
				}
			}

			for each (var leafItem:LegendDisplayItem in legendCtrl.legendItemArray)
			{
				for each (var businessDspItem2:LegendEntryDisplayControl in arrayDspCtrl)
				{
					if (businessDspItem2.parentId == leafItem.legendEntryDspId && businessDspItem2.parentId > 0)
					{
						var tmpItem:LegendDisplayItem=new LegendDisplayItem(businessDspItem2.name);
						tmpItem.legendEntryId=businessDspItem2.leno;
						tmpItem.legendEntryDspId=businessDspItem2.id;

						if (businessDspItem2.leno > 0)
						{
							var businessLegendEntryDef2:LegendEntryDefinition=MetaDataManager.getLegendEntryDefByEntryId(legendId, businessDspItem2.leno);
							tmpItem.levelable=businessLegendEntryDef2.scalable;
							tmpItem.selectable=businessLegendEntryDef2.selectable;
							tmpItem.visible=businessLegendEntryDef2.displayMode;
						}

						tmpItem.parentLegendItem=leafItem;
						legendCtrl.addLegendItem(tmpItem);
					}
				}
			}

			//获取legendentry对应的featurelayer
			for each (var legendDspItem2:LegendDisplayItem in legendCtrl.legendItemArray)
			{
				if (legendDspItem2.legendEntryId > 0)
				{

					var legendEntry:LegendEntry=MetaDataManager.getLegendEntryById(legendDspItem2.legendEntryId);
					if (legendEntry.layerType == LayerTypeConstant.LAYERTYPE_TEXT && legendEntry.bautoLabel)
					{
						var legendEntryReal:LegendEntry=MetaDataManager.getLegendEntryByLabelEntryId(legendDspItem2.legendEntryId);
						legendDspItem2.layer=kgisMap.getLayerById(legendEntryReal.leno) as FeatureLayer;
						legendDspItem2.labelLayer=true;
					}
					else
					{
						legendDspItem2.layer=kgisMap.getLayerById(legendDspItem2.legendEntryId) as FeatureLayer;
					}
				}
			}

			legendCtrl.SetMap(kgisMap);
		}
	}
}